/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.qhq.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.qhq.entity.SchoolDept;
import com.thinkgem.jeesite.modules.qhq.service.SchoolDeptService;

/**
 * 学部管理Controller
 * @author 秦华权
 * @version 2017-11-27
 */
@Controller
@RequestMapping(value = "${adminPath}/qhq/schoolDept")
public class SchoolDeptController extends BaseController {

	@Autowired
	private SchoolDeptService schoolDeptService;
	
	@ModelAttribute
	public SchoolDept get(@RequestParam(required=false) String id) {
		SchoolDept entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = schoolDeptService.get(id);
		}
		if (entity == null){
			entity = new SchoolDept();
		}
		return entity;
	}
	
	@RequiresPermissions("qhq:schoolDept:view")
	@RequestMapping(value = {"list", ""})
	public String list(SchoolDept schoolDept, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SchoolDept> page = schoolDeptService.findPage(new Page<SchoolDept>(request, response), schoolDept); 
		model.addAttribute("page", page);
		return "modules/qhq/schoolDeptList";
	}

	@RequiresPermissions("qhq:schoolDept:view")
	@RequestMapping(value = "form")
	public String form(SchoolDept schoolDept, Model model) {
		model.addAttribute("schoolDept", schoolDept);
		return "modules/qhq/schoolDeptForm";
	}

	@RequiresPermissions("qhq:schoolDept:edit")
	@RequestMapping(value = "save")
	public String save(SchoolDept schoolDept, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, schoolDept)){
			return form(schoolDept, model);
		}
		schoolDeptService.save(schoolDept);
		addMessage(redirectAttributes, "保存学部成功");
		return "redirect:"+Global.getAdminPath()+"/qhq/schoolDept/?repage";
	}
	
	@RequiresPermissions("qhq:schoolDept:edit")
	@RequestMapping(value = "delete")
	public String delete(SchoolDept schoolDept, RedirectAttributes redirectAttributes) {
		schoolDeptService.delete(schoolDept);
		addMessage(redirectAttributes, "删除学部成功");
		return "redirect:"+Global.getAdminPath()+"/qhq/schoolDept/?repage";
	}

}